<?php

class cntrlbuilder implements viewController {
	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "creator.js", "printDialog.js", "picker.js", "numControl.js"), "CSS" => array("facebox.css") );
	private $pageName = "PicBoard Communication Chart Builder";

	public function __construct(user &$userObj){

		if( $userObj->getAccessLevel() == -1 ){
			// User is not logged in
			header("Location: home");
			exit;
		}
		// Main area
		$this->area = new template("area.defaultGrid");
		
		// Side area.
		$cellOptions = new template("control.builderOptions");
		
		$searchControl 	= new template("control.search");
		// Get the name of the default API for search.
		eval("\$apiName = " . SYS_API_DEFAULTEXTENSION . "::getName();");
		$searchControl->replace("symbolSetName", $apiName);

		parse_str( $_GET['act'] );
		
		if(isset($_GET['template'])) $template = $_GET['template'];
		
		if( isset($id) ){
			$chartData = chart::getChart($userObj, $id, true);
			
			// Did we get the chart information successfully and is the user allowed to see it?
			if($chartData['status'] == "success"){	
				$remoteData = $chartData['data'];
				
				// Don't allow editing for public charts
				if($remoteData['chart']['groupID'] == "" && $remoteData['chart']['ownerID'] == "") $remoteData['chart']['chID'] = 0;
					
				$this->dependencies['InlineJavaScript'][] = "var remoteChartData = " . json_encode($remoteData) . ";";
				
				
				// Load template
				$savedTemplate = $chartData['data']['chart']['style'];
			} else {
				header("Location: error?e=166");
			}
		} else {
			$savedTemplate = "defaultFormat";
		}
		
		// Are we converting to a template?
		if( !isset($template) ){
			// Load the saved template / default.
			$template = $savedTemplate;
		}
		
		// Is the template in the allowed templates?
		if(in_array( $template, unserialize(REGISTERED_OUTPUTS) ) == false) throw new PicBoardException("Unrecognised template.");
		// Load the template information.
		$template = new $template();
		
		// Render the changes that the template has made to the chart
		$this->renderTemplate($template);

		if(isset($id)) $this->dependencies['JavaScript'][] = "chartloader.js";

		$this->side = $cellOptions->get() . $searchControl->get();	
	}
	
	private function renderTemplate(printFormatter $template){
		$printFormat = $template->get();

		// Remove print.js
		unset($printFormat['dependencies'][0]);
		
		$merge = array("CSS" => array(), "JavaScript" => array());
		
		// Merge dependencies
		foreach($printFormat['dependencies'] as $dependency){
			if(stristr($dependency, ".js") !== false) array_push($merge['JavaScript'], $dependency);
			
			if(stristr($dependency, ".css") !== false) array_push($merge['CSS'], $dependency);
		}		
		
		if(count($merge['JavaScript']) > 0 ) $this->dependencies['JavaScript'] = array_merge($this->dependencies['JavaScript'], $merge['JavaScript']);
		if(count($merge['CSS']) > 0 ) $this->dependencies['CSS'] = array_merge($this->dependencies['CSS'], $merge['CSS']);
		
		if($template->getFormatName() != "defaultFormat") $this->dependencies['InlineJavaScript'][] = "var templateData = " . json_encode($printFormat) . ";";
	}
	
	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}
	
	public function getPageName(){
		return $this->pageName;
	}

	public function getTools(){
		return new template("toolset.builder");
	}

}

?>